home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / pilot-dedupe.c < prev    next >
C/C++ Source or Header  |  1997-06-13  |  4KB  |  172 lines

  1. /* pilot-dedupe.c:  Pilot utility to remove duplicate records
  2.  *
  3.  * This is free software, licensed under the GNU Public License V2.
  4.  * See the file COPYING for details.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "pi-source.h"
  10. #include "pi-socket.h"
  11. #include "pi-dlp.h"
  12.  
  13. char * progname;
  14.  
  15. void Help(void)
  16. {
  17.   fprintf(stderr,"usage:%s %s dbname [dbname ...]\n",progname,TTYPrompt);
  18.   exit(2);
  19. }
  20.  
  21. struct record {
  22.     unsigned long id;
  23.     char * data;
  24.     int len;
  25.     int cat;
  26.     int index;
  27.     struct record * next;
  28. };
  29.  
  30. struct record * records = 0;
  31.  
  32. int main(int argc, char *argv[])
  33. {
  34.   struct pi_sockaddr addr;
  35.   int db;
  36.   int sd;
  37.   int l;
  38.   struct PilotUser U;
  39.   int ret;
  40.   char buf[0xffff];
  41.   int i;
  42.   struct record * r;
  43. #ifdef sun
  44.   extern char* optarg;
  45.   extern int optind;
  46. #endif
  47.  
  48.   progname = argv[0];
  49.  
  50.   if (argc < 3)
  51.     Help();
  52.  
  53.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  54.     perror("pi_socket");
  55.     exit(1);
  56.   }
  57.     
  58.   addr.pi_family = PI_AF_SLP;
  59.   strcpy(addr.pi_device,argv[1]);
  60.   
  61.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  62.   if(ret == -1) {
  63.     perror("pi_bind");
  64.     exit(1);
  65.   }
  66.  
  67.   ret = pi_listen(sd,1);
  68.   if(ret == -1) {
  69.     perror("pi_listen");
  70.     exit(1);
  71.   }
  72.  
  73.   sd = pi_accept(sd, 0, 0);
  74.   if(sd == -1) {
  75.     perror("pi_accept");
  76.     exit(1);
  77.   }
  78.  
  79.   /* Ask the pilot who it is. */
  80.   dlp_ReadUserInfo(sd,&U);
  81.   
  82.   /* Tell user (via Pilot) that we are starting things up */
  83.   dlp_OpenConduit(sd);
  84.   
  85.   for (i=2;i<argc;i++) {
  86.     int dupe=0;
  87.     /* Open the database, store access handle in db */
  88.     printf("Opening %s\n", argv[i]);
  89.     if(dlp_OpenDB(sd, 0, dlpOpenReadWrite, argv[i], &db) < 0) {
  90.       printf("Unable to open %s\n", argv[i]);
  91.       /*dlp_AddSyncLogEntry(sd, "Unable to open AddressDB.\n");
  92.       exit(1);*/
  93.       continue;
  94.     }
  95.     printf("Reading records...\n");
  96.   
  97.     l=0;
  98.     for(;;) {
  99.       int attr;
  100.       recordid_t id;
  101.       int cat;
  102.       int len = dlp_ReadRecordByIndex(sd, db, l, (unsigned char *)buf, &id, 0, &attr, &cat);
  103.       
  104.       l++;
  105.       
  106.       if(len<0)
  107.         break;
  108.                           
  109.       /* Skip deleted records */
  110.       if((attr & dlpRecAttrDeleted) || (attr & dlpRecAttrArchived))
  111.         continue;
  112.         
  113.       r = (struct record*)malloc(sizeof(struct record));
  114.       r->data = (char*)malloc(len);
  115.       memcpy(r->data, buf, len);
  116.       r->len = len;
  117.       r->cat = cat;
  118.       r->id = id;
  119.       r->index = l;
  120.       
  121.       r->next = records;
  122.       records = r;
  123.       
  124.     }
  125.     
  126.     printf("Scanning for duplicates...\n");
  127.     
  128.     r = records;
  129.     while (r) {
  130.       struct record * r2 = r->next;
  131.       int d = 1;
  132.  
  133.       pi_tickle(sd);
  134.  
  135.       while(r2) {
  136.         if ((r2->len == r->len) && 
  137.             (r2->cat == r->cat) &&
  138.             (memcmp(r2->data,r->data,r->len)==0)) {
  139.             printf("Deleting record %d, duplicate #%d of record %d\n", r2->index, d++, r->index);
  140.             dupe++;
  141.             dlp_DeleteRecord(sd, db, 0, r2->id);
  142.             r2->id = 0;
  143.             r2->len = -1;
  144.         }
  145.         r2=r2->next;
  146.       }
  147.       r=r->next;
  148.     }
  149.     
  150.     while(records) {
  151.       if (records->data)
  152.         free(records->data);
  153.       r = records;
  154.       records=records->next;
  155.       free(r);
  156.     }
  157.     /* Close the database */
  158.     dlp_CloseDB(sd, db);
  159.     sprintf(buf,"Removed %d duplicates from %s\n", dupe, argv[i]);
  160.     printf("%s",buf);
  161.     dlp_AddSyncLogEntry(sd, buf);
  162.   }
  163.  
  164.   dlp_ResetLastSyncPC(sd);
  165.   
  166.   /* All of the following code is now unnecessary, but harmless */
  167.   
  168.   dlp_EndOfSync(sd,0);
  169.   pi_close(sd);
  170.   exit(0);
  171. }
  172.